class User < ActiveRecord::Base
  has_many :microposts, dependent: :destroy
  #email should be downcase when save it into database
  before_save { self.email = email.downcase }
  before_create :create_remember_token
  #name format
  validates :name, presence: true, length: { maximum: 50}
  #email format
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, 
             format: { with: VALID_EMAIL_REGEX},
             uniqueness: { case_sensitive: false }
  validates :password, length: {minimum: 6}
  has_secure_password

  def feed
    #This is preliminary. See "Following users" for the full implementation.
    Micropost.where("user_id = ?", id)
  end

  def User.new_remember_token
    SecureRandom.urlsafe_base64
  end

  def User.encrypt(token)
    Digest::SHA1.hexdigest(token.to_s)
  end

  private
    
    def create_remember_token
      self.remember_token = User.encrypt(User.new_remember_token)
    end
end
